﻿
//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#pragma once

/// \brief 声明一个事件处理对象类的宏，放在类声明中。
#define DECLARE_UI_EVENT_HANDLER(CLASS_NAME)       \
private:                                \
    static bool m_sInstanceDestructorCalled;      \
    static CLASS_NAME* m_pInstance;   \
public:                                 \
    static CLASS_NAME* GetInstance();

/// \brief 事件处理对象析构时需要一些清理工作的宏，放在事件处理对象的析构函数中。
#define UI_EVENT_HANDLER_SHOULD_CALL_ME_FIRSTLY_IN_DESTRUCTOR() \
    m_sInstanceDestructorCalled = true

/// \brief 事件处理对象的实例化的宏，放在类实现文件中。
#define IMPLEMENT_UI_EVENT_HANDLER(CLASS_NAME)     \
    bool CLASS_NAME::m_sInstanceDestructorCalled = false; \
    CLASS_NAME* CLASS_NAME::m_pInstance = nullptr; \
    CLASS_NAME* CLASS_NAME::GetInstance()   \
    {  \
        if (m_sInstanceDestructorCalled) { return nullptr;  } \
        static CLASS_NAME eventHandler;  \
        if (!m_pInstance) \
        { \
            m_pInstance = &eventHandler;  \
            eventHandler.Register();  \
        } \
        return m_pInstance;      \
    }
